﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Data.DBModels;
using Data.MyDbContext;
using DAL;
using Utilities;

namespace WebIndex1.Controllers
{
    public class CategoryController : Controller
    {
        private readonly IUnitOfRepositories _repositories = null;
        public CategoryController(IUnitOfRepositories repositories)
        {
            _repositories = repositories;
        }
        //
        // GET: /Category/

        public ActionResult DisplayAllCategoriesPartial()
        {
            return PartialView("_DisplayAllCategories", _repositories.CategoryRepo.GetAll());
        }

        //
        // GET: /Category/Edit/5
        [Authorize]
        public ActionResult EditPopUp(Guid? id)
        {
            Category category = new Category();
            if (id.IsValid())
            {
                category = _repositories.CategoryRepo.Find(id.Value);
                if (category == null || Helper.GetUser(_repositories.UserRepo) != category.CreatedBy)
                    return new EmptyResult();
            }
            
            return PartialView("_editPopup",category);
        }

        //
        // POST: /Category/Edit/5
        [Authorize]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult EditPopUp(Category category)
        {
            if (ModelState.IsValid)
            {
                
                category.ModifiedOn = DateTime.Now;
                
                
                if (category.Id.IsValid())
                {
                    

                    Category categoryToUpdate = _repositories.CategoryRepo.Find(category.Id);

                    if (Helper.GetUser(_repositories.UserRepo) != categoryToUpdate.CreatedBy)
                        return new EmptyResult();

                    TryUpdateModel(categoryToUpdate, "", null, new string[] { "CreatedOn", "CreatedBy_Id", "CreatedBy" });

                    _repositories.CategoryRepo.Update(categoryToUpdate);
                    Category newCate = _repositories.CategoryRepo.Find(category.Id);
                    return PartialView("_DisplayWebsitesInCategory", newCate);
                }
                else
                {
                    category.Id = Guid.NewGuid();
                    category.CreatedOn = DateTime.Now;
                    category.CreatedBy = Helper.GetUser(_repositories.UserRepo);
                    _repositories.CategoryRepo.Add(category);
                    return RedirectToAction("DisplayAllCategoriesPartial", "Category");
                }
                
                
            }
            return PartialView("_editPopup",category);
        }

        //
        // GET: /Category/Delete/5

        public ActionResult Delete(Guid id)
        {
            Category category = _repositories.CategoryRepo.Find(id);
            if (category == null)
            {
                return HttpNotFound();
            }
            return View(category);
        }

        public ActionResult AddWebsites()
        {
            return View();
        }

        //
        // POST: /Category/Delete/5

        [Authorize]
        [HttpPost, ActionName("Delete")]
        //[ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(Guid id)
        {
            if (_repositories.CategoryRepo.Delete(id)) return RedirectToAction("DisplayAllCategoriesPartial", "Category");

            return View();
        }

        public ActionResult CreateCategoriesDDL(List<Category> categories = null)
        {
            List<SelectListItem> items = new List<SelectListItem>();

            if (categories == null) categories = _repositories.CategoryRepo.GetAll();
            foreach (Category c in categories)
            {
                items.Add(new SelectListItem
                {
                    Text = c.Name,
                    Value = c.Id.ToString()
                });

            }

            items.Insert(0, new SelectListItem { Text = "", Value = null });

            return PartialView("_Categories", items);
        }

    }
}